#!/usr/bin/env python3
# coding:utf8
import csv
import unittest
import time

import os

from basis.basis_driver import BasisBrowser, BasisDriver
from config.text_config import open_tkinter
from pages.login_page import LoginPage
from pages.search_page import SearchPage


class WebTestLogin(unittest.TestCase,LoginPage):

    Adress = '192.168.1.195'
    BASIS_BROWSER = BasisBrowser.Chrome
    path = os.path.abspath(os.path.dirname(os.getcwd()))
    '''使用basis_driver'''
    def setUp(self):
        # self.URL = "http://" + self.Adress + ":8081/bi"
        self.URL = "http://www.shunyingglobal.cn/Account/Login"
        self.driver = BasisDriver(self.BASIS_BROWSER)
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        self.base_url = self.URL
        self.LOGINPAGE = LoginPage(self.driver,self.base_url)   #实例化LoginPage作为局部变量时使用
        self.LOGINPAGE.open("")
        open_tkinter("/data/huozhan.gif",
                                 '\n -----------模拟用户搜索商品-----------\n',
                                 '''
        1、全局搜索(获取全部展示商品编码)
        2、模糊搜索(暂未做自动化)
        3、精准搜索(暂未做自动化)
        4、热词搜索(暂未做自动化)
        5、分词搜索（暂无此功能）
                                 ''')
        LOGINPAGE = LoginPage(self.driver, self.base_url)
        LOGINPAGE.login('15020579521', '123456')
        self.driver.sleep(2)
    def tearDown(self):
        self.driver.quit_browser()

    def test_search(self):
        search = SearchPage(self.driver,self.base_url)
        search.search_product()

    # '''使用csv文件传参'''
    # def test_csv_login(self):
    #     self.driver.xlsx_csv("/data/case.xlsx", 'login', "/data/csv/login.csv")
    #     self.log("打开csv文件")
    #     self.csv_file = open(self.path + r'/data/csv/login.csv',  # csv文件路径
    #                          mode='r',  # 获得文件权限
    #                          encoding='utf8')  # 字符集
    #
    #     self.log("读取csv文件")
    #     csv_data = csv.reader(self.csv_file)
    #
    #     self.log("跳过第一行数据")
    #     is_header = True
    #     for line in csv_data:
    #         if is_header:
    #             is_header = False
    #             continue    #跳出本次循环
    #
    #         self.log('csv文件数据生成字典')
    #         data = {'number':line[0],
    #                 'username':line[1],
    #                 'password':line[2],
    #                 'casetype':line[3]}
    #
    #         self.log("实例化LoginPage作为局部变量")
    #         LOGINPAGE = LoginPage(self.driver,self.base_url)
    #         self.log("调用对象LoginPage中的方法login_inherit并传递(data dict)参数")
    #         LOGINPAGE.login_inherit(data)
    #         # try:
    #         #     self.log("开始执行断言")
    #         #     if data['casetype'] == 'True':
    #         #         self.log("获取断言文本1")
    #         #         TITLE = LOGINPAGE.title
    #         #         self.driver.assertion_log(TITLE,
    #         #                                   "供应链",
    #         #                                   "\nTrue断言失败:" + data['number']+
    #         #                                   "\n断言生成时间："+
    #         #                                   (time.strftime("%Y/%m/%d-%H:%M:%S",time.localtime()))+"\n",
    #         #                                   r'/screenshots',
    #         #                                   r'/logs_assert/assert_true.txt')
    #         #
    #         #
    #         #     if data['casetype'] == 'False':
    #         #         self.log("获取断言文本2")
    #         #         text = LOGINPAGE.FAIL_TEXT
    #         #         self.log("执行登录失败断言")
    #         #
    #         #         self.driver.assertion_log(text,
    #         #                                   " 账户登录 ",
    #         #                                   "\nFalse断言失败:" +
    #         #                                   data['number']+"\n断言生成时间："+
    #         #                                   (time.strftime("%Y/%m/%d-%H:%M:%S",time.localtime()))+"\n",
    #         #                                   r'/screenshots',
    #         #                                   r'/logs_assert/assert_false.txt')
    #         # except:
    #         #     print('跳过断言:'+ data['number'])
    #
    #     self.log("关闭csv文件")
    #     # self.csv_file.close()

if __name__ == '__main__':
    unittest.main()
